package com.learn.springboot.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 配置全局异常处理控制类
 * @author wangxing
 * @version 2020/8/3 11:26 Administrator
 */
//增强的Controller
@ControllerAdvice
public class HandlerException {
    @ExceptionHandler(value = ServiceException.class)
    @ResponseBody
    public RetrunException handleService(HttpServletRequest request, ServiceException e){
        RetrunException retrunException = new RetrunException();
        retrunException.setCode(600);
        retrunException.setMsg(e.getMessage());
        retrunException.setUrl(String.valueOf(request.getRequestURI()));
        return retrunException;
    }
    @ExceptionHandler(value = Exception.class)
    public ModelAndView handlerAll(HttpServletRequest request, Exception e){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("ExeMsg",e.getMessage());
        modelAndView.addObject("ReqUrl",request.getRequestURI());
        modelAndView.setViewName("/exeMsg");
        return modelAndView;
    }
}
